package com.lcx.plugin.enums;

import cn.hutool.core.util.StrUtil;
import com.lcx.plugin.entity.TypeMapper;
import lombok.AllArgsConstructor;
import lombok.Getter;

import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @author daxiong
 * @date 2023/6/15 下午5:06
 */
@Getter
@AllArgsConstructor
public enum MysqlTypeMatch {
    //
    INT_64("int64", "bigint", "0"),
    INT_32("int32", "int", "0"),
    INT_24("int24", "mediumint", "0"),
    INT_16("int16", "smallint", "0"),
    INT_8("int8", "tinyint", "0"),
    STRING("string", "varchar", "255"),
    DATETIME("datetime", "datetime", "2"),
    DATE("date", "date", "0"),
    TIME("time", "time", "0"),
    DOUBLE("double", "double", "10,2"),

    ;

    private final String excelType;
    private final String jdbcType;
    private final String defaultLength;

    /**
     * 如果找不到类型，则默认为string类型
     *
     * @param excelType
     * @return com.sjhy.plugin.enums.SqlTypeMatch
     * @author daxiong
     * @date 2023/6/15 下午5:17
     */
    public static MysqlTypeMatch getByExcelType(String excelType) {
        if (StrUtil.isBlank(excelType)) {
            return STRING;
        }
        for (MysqlTypeMatch value : values()) {
            if (Objects.equals(excelType.toLowerCase(), value.getExcelType())) {
                return value;
            }
        }
        return STRING;
    }

    public static List<TypeMapper> defaultValues() {
        return Arrays.stream(values()).map(TypeMapper::new).collect(Collectors.toList());
    }
}
